package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("adminSetmealController")
@Slf4j
@RequestMapping("/admin/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    /**
     * 分页查询套餐信息
     * @param setmealPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询套餐信息")
    public Result<PageResult> save(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("当前要查询的页码数据:{}", setmealPageQueryDTO);
        PageResult result = setmealService.save(setmealPageQueryDTO);
        return Result.success(result);
    }

    /**根据id查询套餐
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐")
    public Result<SetmealVO> findById(@PathVariable Long id) {
        log.info("当前要查询的套餐id为:{}", id);
        SetmealVO findById = setmealService.findById(id);
        return Result.success(findById);
    }

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增套餐")
//    清除缓存数据
    @CacheEvict(cacheNames = "setmealCache", key = "#{setmealDTO.categoryId}")

    public Result addSetmeal(@RequestBody SetmealDTO setmealDTO) {
        log.info("新增的套餐信息为:{}", setmealDTO);
        setmealService.addSetmeal(setmealDTO);
        return Result.success();
    }

    /**
     * 修改套餐
     * @param setmealDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改套餐")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result modifySetmeal(@RequestBody SetmealDTO setmealDTO) {
        log.info("修改的商品信息为:{}", setmealDTO);
        setmealService.modifySetmeal(setmealDTO);
        return Result.success();
    }

    /**
     * 套餐起售或停售
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("套餐起售或停售")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result startOrStop(@PathVariable Integer status, Long id) {
        log.info("当前切换的状态为:{},当前套餐id为:{}", status, id);
        setmealService.startOrStop(status, id);
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("删除套餐")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result dropSetmeal(@RequestParam List<Long> ids) {
        log.info("当前删除的套餐id为:{}", ids);
        setmealService.dropSetmeal(ids);
        return Result.success();
    }
}
